library Wave16a initializer init requires LegacySystem
        
    /*
    GlobalBoolean [2] - Marines are securing LZ
    GlobalPoint [13]   -  LZ Beacon Location
    */
    
    
    globals
        private trigger LZLandingSequenceTrigger = null
        private trigger LZLandingPingTrigger = null
        
        private rect                    udg_CurrentLZ              = null
        private unit                    udg_LZBeacon               = null
        private integer                 udg_LZSecured              = 0

    endglobals
    
    
    function GetSecuredLandingZones takes nothing returns integer
        return udg_LZSecured
    endfunction
    
    private  function LZGenerator takes nothing returns nothing
        local integer i = GetRandomInt(1,8)
        local real x = GetRectCenterX(udg_LandingZones[i])
        local real y = GetRectCenterY(udg_LandingZones[i])
        if udg_CurrentWave!=16 then
            return
        endif
        set udg_CurrentLZ = udg_LandingZones[i]
        set udg_LZPoint = Location(x,y)
        call MoveRectToLoc(gg_rct_CurrentLZRegion,udg_LZPoint)
        call RemoveLocation(udg_CoreObjectivePoint)
        set udg_CoreObjectivePoint = udg_LZPoint
        call StartSound(gg_snd_SatelliteImaging)
        call DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "|cffcd950cALICE|r : Transmitting position of LZ " + udg_LZNames[udg_LZSecured] )
        call SetCameraQuickPosition(GetLocationX(udg_LZPoint), GetLocationY(udg_LZPoint))
        call PingMinimapEx(x,y,45.0,255,0,0,false)
        call EnableTrigger(LZLandingSequenceTrigger)
        call EnableTrigger(LZLandingPingTrigger)
    endfunction

    private function LandingBeaconActivate takes nothing returns nothing
        call SetPlayerTechResearched(GetEnumPlayer(),'Roen',1)
    endfunction

    private function LandingSequence takes nothing returns nothing
        local integer i = 1
        local integer id
        local integer itemid = 0
        local real angle
        local unit u
        local real x = GetLocationX(udg_LZPoint)
        local real y = GetLocationY(udg_LZPoint)
        local real theta = GetRandomReal(0.0,6.28)
        local real ux = Clamp(x+3000.0*Cos(theta),GetRectMinX(bj_mapInitialPlayableArea),GetRectMaxX(bj_mapInitialPlayableArea))
        local real uy = Clamp(y+3000.0*Sin(theta),GetRectMinY(bj_mapInitialPlayableArea),GetRectMaxY(bj_mapInitialPlayableArea))
        local unit blackhawk = CreateUnit(Player(11),'h009',ux,uy,theta+3.14)
        set udg_LZSecured=udg_LZSecured+1
        call SetForceAllianceStateBJ( bj_FORCE_PLAYER[9], udg_UnknownEntities, bj_ALLIANCE_ALLIED )
        call UnitApplyTimedLife(blackhawk,'Bhwd',60.0)
        call SetUnitScalePercent(blackhawk,50.0,50.0,50.0)
        call RemoveGuardPosition(blackhawk)
        call TriggerSleepAction(0.)
        call IssuePointOrder(blackhawk, "move",x,y)
        call StartSound(gg_snd_BlackHawkRotor)
        call UnitAddAbility(udg_LZBeacon,'Aloc')
        set u = CreateUnit(Player(14),'n00P',x,y,0.0) //DangerZone Dummy
        call UnitApplyTimedLife(u,'Bhwd',9.0)
        call PolledWait(2.25)
        call StartSound(gg_snd_BlackHawk)
        call PolledWait(3.25)
        call SetUnitFlyHeight(blackhawk,15.0,130.0)
        call SmoothUnitResize(blackhawk,1.45,1.55,4)
        call SetLandingAreaWindEnabled(true)
        call PolledWait(3.)
        call SetUnitTimeScalePercent(blackhawk,65.0)
        call HeloShock(blackhawk)
        call BlackHawkWashOff.execute()
        call PlaySoundAtPointBJ( gg_snd_BlackHawkLand, 100.0, udg_LZPoint, 15.0 )
        call PolledWait(2.5)
        call KiloEvac(blackhawk)
        call IssueImmediateOrder(blackhawk,"holdposition")
        loop
            exitwhen i>udg_InitialPlayers
            set angle = 360.0*(I2R(i)-1) / I2R(udg_InitialPlayers)
            if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[i]))!=PLAYER_SLOT_STATE_LEFT then
                if GetRandomInt(0,12)==1 and udg_Experience[udg_Sorted[i]]>60000 then
                    set id = 'z00E' //IUL
                elseif GetRandomInt(0,60)==1 and udg_Experience[udg_Sorted[i]]>60000 then
                    set id = 'z006' //Pilot
                elseif GetRandomInt(0,12)==1 and udg_Experience[udg_Sorted[i]]>30000 then
                    set id = 'z00G' //Sniper
                elseif GetRandomInt(0,4)==1 then
                    set id = 'z009' //Flamethrower
                    set itemid = 'I003' //Hecate
                elseif GetRandomInt(0,6)==1 then
                    set id = 'z001' //Corpsman
                    set itemid = 'I00A' //Blastocyst
                elseif GetRandomInt(0,10)==1 then
                    set id = 'z004' //Engineer
                    set itemid = 'I00W' //Flare Gun
                elseif GetRandomInt(0,3)==1 then
                    set id = 'z000' //Machine Gunner
                    set itemid = 'I002' //AP
                elseif GetRandomInt(0,5)==1 then
                    set id = 'z00F' //Marksman
                else
                    set id = 'z008' //Rifleman
                    set itemid = 'I002' //AP
                endif
                set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),id,x+80.0,y-175.0,0.0)
                call SetLoadState(I2S(GetHandleId(u)),1)
                call UnitAddItemById(u,'I004') //Medkit
                call UnitAddItemById(u,'I001') //Composite
                if itemid!=0 then
                    call UnitAddItemById(u,itemid)
                    set itemid = 0
                endif
                if id=='z001' then
                  //  call TriggerRegisterUnitEvent(gg_trg_Anti_Venom,u,EVENT_UNIT_SPELL_EFFECT)
                elseif id=='z006' then
                    //call TriggerRegisterUnitEvent(gg_trg_Control_Aircraft,u,EVENT_UNIT_SPELL_EFFECT)
                elseif id=='z004' then
                    //call TriggerRegisterUnitEvent(gg_trg_Nanosteel_Net,u,EVENT_UNIT_SPELL_EFFECT)
                endif
                call RegisterUnitForAilmentChecks(u)
                //call TriggerRegisterUnitEvent(gg_trg_Article_15,u,EVENT_UNIT_DEATH)
                call IssuePointOrder(u,"attack",x+(325.0*Cos(angle*0.01745)),y+(325.0*Sin(angle*0.01745)))
            endif
            if GetRandomInt(0,1)==1 then
                call TriggerSleepAction(0.)
            endif
            set i=i+1
        endloop
		set udg_soundpath = "war3mapImported\\LockAndLoad.wav"
		call PlaySoundPath.execute()
        //call StartSound(gg_snd_RockandRoll)
        call KiloEvac(blackhawk)
        call PolledWait(2.)
        call BlackHawkWashOff.execute()
        call SetUnitFlyHeight(blackhawk,800.0,200.0)
        call SmoothUnitResize(blackhawk,1.55,1.45,3)
        call IssuePointOrder(blackhawk,"move",13000.0,10000.0)
        call SetUnitTimeScalePercent(blackhawk,100.0)
        call PolledWait(3.5)
        call SetLandingAreaWindEnabled(false)
        call EnumDestructablesInCircleBJ(600,udg_LZPoint,function TreeShakeEnd)
        call PolledWait(3.)
        call SetForceAllianceStateBJ( bj_FORCE_PLAYER[9], udg_UnknownEntities, bj_ALLIANCE_UNALLIED )
        call RemoveUnit(blackhawk)
        set blackhawk = null
        call RemoveUnit(udg_LZBeacon)
		set udg_LZBeacon = null
        call RemoveLocation(udg_LZPoint)
        call PolledWait(1.)
        set u = null
        if udg_CurrentWave==16 and udg_Players>0 then
            call LZGenerator.execute()
        endif
    endfunction

    private function LZSecureConditions takes nothing returns boolean
        return GetSpellAbilityId()=='A01A'
    endfunction

    private function LZSecure takes nothing returns nothing
        local group udg_mygroup
        local integer i = 1
        local location l = GetSpellTargetLoc()
        local real x = GetLocationX(l)
        local real y = GetLocationY(l)
        if RectContainsCoords(gg_rct_CurrentLZRegion,x,y) then
            call DisableTrigger(LZLandingSequenceTrigger)
            call DisableTrigger(LZLandingPingTrigger)

            set udg_GlobalBoolean[2] = true
            set udg_mygroup = GetUnitsOfTypeIdAll('o000')
            call ForGroup(udg_mygroup,function RemoveUnitAll)
            call DestroyGroup(udg_mygroup)
            call TriggerSleepAction(0.)
            set udg_LZBeacon = CreateUnit(GetTriggerPlayer(),'o000',x,y,0.0)
            call GeneralText(5.0, "Securing LZ.." )
            call CreateTimerDialogBJ(GetLastCreatedTimerBJ(), "Securing LZ" )
            call StartTimerBJ(GetLastCreatedTimerBJ(),false,25.0)
            call TimerDialogDisplay( GetLastCreatedTimerDialogBJ(), true )
            call GankRectCenter(udg_CurrentLZ,Player(14),'n005',udg_Spawn,1800,2300)
            call PolledWait(10.)
            call GankRectCenter(udg_CurrentLZ,Player(14),'n005',udg_Spawn,1800,2300)
            call PolledWait(15.)
            call TimerDialogDisplay( GetLastCreatedTimerDialogBJ(), false )
            if GetWidgetLife(udg_LZBeacon)>0.405 then
                call LandingSequence.execute()
                call SetUnitVertexColorBJ(udg_LZBeacon,0.0,100.0,0.0,25.0)
                set udg_DMKills[1] = udg_DMKills[1] + 1
                if udg_DMKills[1]==2 and udg_Mode==1 then
                    call ALICEText(10.0, "Two Lima Zulus secured. |cff1e90ff100 XP|r awarded to all Marines." )
                    loop
                        exitwhen i>udg_InitialPlayers
                        if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[i]))!=PLAYER_SLOT_STATE_LEFT then
                            set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] + 100
                        endif
                        set i=i+1
                    endloop
                endif
            else
                call GeneralText(5.0, "You have failed to secure the LZ." )
                call PolledWait(5.)
                call RemoveLocation(udg_LZPoint)
                if udg_CurrentWave==16 and udg_Players>0 then
                    call LZGenerator.execute()
                endif
            endif
        else
            call IssueImmediateOrder(GetTriggerUnit(),"holdposition")
            call DisplayTimedTextToPlayer(GetTriggerPlayer(),0.0,0.0,3.0, "Landing Beacon must be placed within designated LZ." )
        endif
        call RemoveLocation(l)
        set l = null
    endfunction

    private function Trig_LZ_Coordinates_Actions takes nothing returns nothing
        local real x = GetLocationX(udg_LZPoint)
        local real y = GetLocationY(udg_LZPoint)
        call SetCameraQuickPosition(x,y)
        call PingMinimapEx(x,y,20.0,255,0,0,false)
    endfunction

    private function Wave16a takes nothing returns nothing
        local integer i = 1
        set udg_CurrentWave = 16
        call GenericActions.execute()

        set udg_LZSecured = 0
        if LZLandingSequenceTrigger == null then
            set LZLandingSequenceTrigger = CreateTrigger()
            call DisableTrigger(LZLandingSequenceTrigger)
            call TriggerAddCondition(LZLandingSequenceTrigger, Condition(function LZSecureConditions) )
            call TriggerAddAction(LZLandingSequenceTrigger,function LZSecure)
            call TriggerRegisterAnyUnitEventBJ(LZLandingSequenceTrigger,EVENT_PLAYER_UNIT_SPELL_CAST)
        endif
        
        if LZLandingPingTrigger == null then
            set LZLandingPingTrigger = CreateTrigger()
            call DisableTrigger( LZLandingPingTrigger )
            call TriggerRegisterTimerEvent(LZLandingPingTrigger,35.0,true)
            call TriggerAddAction( LZLandingPingTrigger, function Trig_LZ_Coordinates_Actions )
        endif

        set udg_DMKills[1] = 0
        set udg_ZStart = 1200
        set udg_ZEnd = 2825
        call FogTransition(0.40)
        call GeneralText(5.0, "|cffcd950cBattalion|r : 160th SOAR is ready for infiltration missions in your sector. Secure as many Landing Zones as you can." )
        call PolledWait(8.)
        call ForForce( udg_EchoCompany, function LandingBeaconActivate )
        call GeneralText(5.0, "|cffcd950cBattalion|r : Coordinates of designated LZs are being transfered to your ALICE unit." )
        call RandomSpawn('u004',2, GetRandomInt(1,udg_Players) ) //Wraith
        call PolledWait(7.)
        call FogTransition(0.45)
        call MultiboardSetTitleText( udg_SquadStatus, "Iron Hand : Secure at least 2 LZs." )
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Secure LZs" )
        call QuestSetDescription(udg_CurrentOrders, "Clear and mark landing zones for 160th Special Operations Aviation Regiment UH-60 Black Hawks using Infrared Strobes. Subsequently, defend the LZ for 30 seconds until the Black Hawk arrives." )
        call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Secure LZs." )
        call MissionUpdate.execute()
        call PolledWait(7.)
        call FogTransition(0.5)
        call GeneralText(5.0, "|cffcd950cMarine|r : We need all the help we can get. Head to the LZs and mark 'em with an IR Strobe." )
        call LZGenerator.execute()
        call PolledWait(15.)
        call GankMarine(Player(14),'u005',2*udg_Spawn,1200,1800)
        call FogTransition(0.55)
        call GeneralText(5.0, "|cffcd950cBattalion|r : Command Authorization for Infrared Strobes granted. Input Z-B." )
        call PolledWait( GetRandomReal(10.0, 100.0) )
        if GetRandomInt(0,1)==1 then
            call GeneralText(3.5, "|cffcd950cBattalion|r : FACs spotted a Thanatos, be on the watch. You will need a lot of firepower to take it down. No further information." )
        endif
        call StartSound(gg_snd_Thanatos)
        set udg_myloc = GetRandomLocInRect(udg_PlayerSpawnBoundary)
        if udg_Players<=4 and udg_HitsTaken>130 then
            set udg_Boss = CreateUnitAtLoc(Player(14),'U00D',udg_myloc,0) //Thanatos Variant A
        else
            set udg_Boss = CreateUnitAtLoc(Player(14),'U006',udg_myloc,0) //Thanatos Variant B
        endif
        call SuspendHeroXP(udg_Boss,true)
        call RemoveLocation(udg_myloc)
        call PolledWait(20.0*udg_Pace)
        call FogTransition(0.60)
        call GeneralText(10.0, "|cffcd950cCaptain Roth|r : Move Move! I want those LZs secured." )
        call PolledWait(100.)
        call SetPopperExplosionControlEnabled(true)
        call GankMarine(Player(14),'u00U',2+udg_Spawn,2500,3500) //Popper sequence
        call GankMarine(Player(13),'u00U',udg_Spawn,2500,3500)
        call FogTransition(0.65)
        set i = GetRandomInt(1, udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]], 5, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : Stay away from the tree lines!" )
        call PolledWait(100.0*udg_Pace)
        call SetPopperExplosionControlEnabled(false)
        call FogTransition(0.70)
        call DisableTrigger(LZLandingSequenceTrigger)
        loop
            exitwhen udg_LZBeacon == null or GetWidgetLife(udg_LZBeacon)<0.405
            call PolledWait(10.)
        endloop
        call GeneralText(10.0, "|cffcd950cBattalion|r : There will be no further landings." )
        call PolledWait(10.)
        if udg_Players>0 then
            call ExecuteRegisteredFunction("Wave17a")
            //call Wave17a.execute()
        endif
        call DisableTrigger(LZLandingSequenceTrigger)
        call DisableTrigger(LZLandingPingTrigger)
    endfunction


    private function init takes nothing returns nothing
        call RegisterFunction("Wave16a", function Wave16a)
    endfunction

endlibrary